package com.ruoyi.web.controller.doctor;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.doctor.domain.DoctorPatient;
import com.ruoyi.doctor.domain.DoctorPeople;
import com.ruoyi.doctor.service.DoctorPatientService;
import com.ruoyi.system.domain.SysRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/system/patient")
public class DoctorPatientController extends BaseController {

    @Autowired
    DoctorPatientService service;

    //查询患者的科室
    //@PreAuthorize("@ss.hasPermi('system:patient:list')")
    @GetMapping("/findPatient")
    public AjaxResult one(String registerId)
    {
        DoctorPatient sysPatient = service.queryBySfz(registerId);
        if(sysPatient == null){
            return AjaxResult.error("数据库中没有该患者");
        }
        return AjaxResult.success(sysPatient);
    }


    //查询挂号费用
    @GetMapping("/queryRegis")
    public AjaxResult queryFee(Long regisId,int patientId)
    {
        System.out.println(regisId);
        System.out.println(patientId);
        SysRegister sysRegis = service.queryFee(regisId,patientId);
        return AjaxResult.success(sysRegis);
    }

    @GetMapping("/addDept")
    public AjaxResult addDept(int patientId,int deptId,int beforeCode)
    {
        System.out.println("patientId = " + patientId + ", deptId = " + deptId + ", beforeCode = " + beforeCode);
        int i = service.addDept(patientId, (long) deptId);
        service.updatePeople(deptId, beforeCode);
        if(i != 0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }



    @PreAuthorize("@ss.hasPermi('system:patient:list')")
    @GetMapping("/listDepts")
    public TableDataInfo list(String deptName)
    {
        startPage(); //封装好了的分页
        List<DoctorPeople> xlPeople = service.queryByDept(deptName);
        //System.out.println(sysDepts);
        return getDataTable(xlPeople); //封装好了的返回值
    }


    @GetMapping("/updatePeople")
    public AjaxResult updatePeople(int deptId,int beforecode)
    {
        System.out.println("deptId = " + deptId + ", beforecode = " + beforecode);
        //System.out.println(patientId+":"+deptId);
        int i = service.updatePeople(deptId, beforecode);
        if(i != 0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @PostMapping("/addPatient")
    public int addPatient(DoctorPatient patient){
        return service.addPatient(patient);
    }

}
